package net.zjitc.servlet;

import net.zjitc.domain.AdminUser;
import net.zjitc.service.AdminService;
import net.zjitc.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/10/25 11:34
 * 4
 */
@WebServlet(name = "loginServlet",urlPatterns = "/loginServlet")
public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String loginName = request.getParameter("username");
        String loginPsw = request.getParameter("password");
        String loginCheck = request.getParameter("check");
        String lc = (String) request.getSession().getAttribute("loginCheck");
        AdminService service = new AdminServiceImpl();
        try {
            AdminUser user = service.login(loginName,loginPsw);
            HttpSession session = request.getSession();
            PrintWriter writer = response.getWriter();
            if (user != null && loginCheck.equals(lc))
            {
                session.setAttribute("user",user);
                request.getRequestDispatcher("/main.jsp").forward(request,response);
            } else {
                request.setAttribute("msg","用户名或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
